﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;
using System.Reflection;
using DevExpress.Web.ASPxUploadControl;
using System.IO;

namespace EnterpriseFund.Controllers
{
        [OutputCache(Duration = 0, VaryByParam = "none")]   
    public class TaxManagerController : AdminBaseController
    {
        //
        // GET: /TaxManager/
        public ActionResult Index(string NSRBM, string NSRSBH, string NSRMC, string HY_DM, string taxClassNo, string YSKM_DM, string FDataSource, string ZSFS_DM, string SKSX_DM, string RKRQBegin, string RKRQEnd, string SSSQBegin, string SSSQEnd)
        {
            string strwhere = "1=1";
            BLL.VSBZSXX VSBZSXXBLL = new BLL.VSBZSXX();
            List<Model.VSBZSXX> VSBZSXXList = new List<Model.VSBZSXX>();
            int pageIndex = 1;
            if (RKRQBegin != "" && RKRQBegin != null && RKRQEnd != "" && RKRQEnd != null)
            {
                strwhere += " and  DATEDIFF(DAY," + RKRQBegin + ",RKRQ)>0 and DATEDIFF(DAY," + RKRQEnd + ",RKRQ)<0";
                ViewData["RKRQBegin"] = RKRQBegin;
                ViewData["RKRQEnd"] = RKRQEnd;
            }
            else if (RKRQBegin != "" && RKRQBegin != null)
            {
                strwhere += " and  DATEDIFF(DAY," + RKRQBegin + ",RKRQ)>0 ";
                ViewData["RKRQBegin"] = RKRQBegin;
            }
            else if (RKRQEnd != "" && RKRQEnd != null)
            {
                strwhere += " and DATEDIFF(DAY," + RKRQEnd + ",RKRQ)<0";
                ViewData["RKRQEnd"] = RKRQEnd;
            }

            if (SSSQBegin != "" && SSSQBegin != null && SSSQEnd != "" && SSSQEnd != null)
            {
                strwhere += " and  DATEDIFF(month," + SSSQBegin + ",SSSQ_Q)>0 and DATEDIFF(month," + SSSQEnd + ",SSSQ_Q)<0";

                ViewData["SSSQBegin"] = SSSQBegin;

                ViewData["SSSQEnd"] = SSSQEnd;
            }
            else if (SSSQBegin != "" && SSSQBegin != null)
            {
                strwhere += " and  DATEDIFF(month," + RKRQBegin + ",SSSQ_Q)>0 ";
                ViewData["SSSQBegin"] = SSSQBegin;
            }
            else if (SSSQEnd != "" && SSSQEnd != null)
            {
                strwhere += " and DATEDIFF(month," + SSSQEnd + ",SSSQ_Q)<0";
                ViewData["SSSQEnd"] = SSSQEnd;
            }


            if (SKSX_DM != null && SKSX_DM != "")
            {
                strwhere += " and  SKSX_DM = '" + SKSX_DM + "'";
                ViewData["SKSX_DM"] = SKSX_DM;
            }
            if (ZSFS_DM != null && ZSFS_DM != "")
            {
                strwhere += " and  ZSFS_DM = '" + ZSFS_DM + "'";
                ViewData["ZSFS_DM"] = ZSFS_DM;
            }
            if (FDataSource != null && FDataSource != "")
            {
                strwhere += " and  FDataSource = '" + FDataSource + "'";
                ViewData["FDataSource"] = FDataSource;
            }
            if (NSRBM != null && NSRBM != "")
            {
                strwhere += " and  NSRBM like '%" + NSRBM + "%'";
                ViewData["NSRBM"] = NSRBM;
            }
            //if (NSRSBH != null && NSRSBH != "")
            //{
            //    strwhere += " and  NSRSBH like '%" + NSRSBH + "%'";
            //}
            if (NSRMC != null && NSRMC != "")
            {
                strwhere += " and  NSRMC like '%" + NSRMC + "%'";
                ViewData["NSRMC"] = NSRMC;
            }
            if (HY_DM != null && HY_DM != "")
            {
                strwhere += " and  HY_DM = '" + HY_DM + "'";
                ViewData["HY_DM"] = HY_DM;
            }
            if (taxClassNo != null && taxClassNo != "")
            {
                strwhere += " and  ZSXM_DM = '" + taxClassNo + "'";
                ViewData["TaxClassNo"] = taxClassNo;
            }
            if (YSKM_DM != null && YSKM_DM != "")
            {
                strwhere += " and  YSKM_DM = '" + YSKM_DM + "'";
                ViewData["YSKM_DM"] = YSKM_DM;
            }
            VSBZSXXList = GetVSBZSXXListList(strwhere, pageIndex);
            return View(VSBZSXXList);
        }


        /// <summary>
        /// 编码树相关调用方法 主要用于刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult TaxManagerListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string RKRQBegin = Request.Params["RKRQBegin"];
            string RKRQEnd = Request.Params["RKRQEnd"];
            string SSSQBegin = Request.Params["SSSQBegin"];
            string SSSQEnd = Request.Params["SSSQEnd"];
            string SKSX_DM = Request.Params["SKSX_DM"];
            string ZSFS_DM = Request.Params["ZSFS_DM"];

            string FDataSource = Request.Params["FDataSource"];
            string NSRBM = Request.Params["NSRBM"];
            string NSRMC = Request.Params["NSRMC"];
            string HY_DM = Request.Params["HY_DM"];
            string taxClassNo = Request.Params["TaxClassNo"];
            string YSKM_DM = Request.Params["YSKM_DM"];

            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = "";
            BLL.VSBZSXX VSBZSXXBLL = new BLL.VSBZSXX();
            List<Model.VSBZSXX> VSBZSXXList = new List<Model.VSBZSXX>();

            if (RKRQBegin != "" && RKRQBegin != null && RKRQEnd != "" && RKRQEnd != null)
            {
                strwhere += " and  DATEDIFF(DAY," + RKRQBegin + ",RKRQ)>0 and DATEDIFF(DAY," + RKRQEnd + ",RKRQ)<0";
            }
            else if (RKRQBegin != "" && RKRQBegin != null)
            {
                strwhere += " and  DATEDIFF(DAY," + RKRQBegin + ",RKRQ)>0 ";
            }
            else if (RKRQEnd != "" && RKRQEnd != null)
            {
                strwhere += " and DATEDIFF(DAY," + RKRQEnd + ",RKRQ)<0";
            }

            if (SSSQBegin != ""&& SSSQBegin!=null&& SSSQEnd != ""&& SSSQEnd!=null)
            {
                strwhere += " and  DATEDIFF(month," + SSSQBegin + ",SSSQ_Q)>0 and DATEDIFF(month," + SSSQEnd + ",SSSQ_Q)<0";
            }
            else if (SSSQBegin != "" && SSSQEnd != null)
            {
                strwhere += " and  DATEDIFF(month," + SSSQBegin + ",SSSQ_Q)>0 ";
            }
            else if (SSSQEnd != "" && SSSQEnd!=null)
            {
                strwhere += " and DATEDIFF(month," + SSSQEnd + ",SSSQ_Q)<0";
            }


            if (SKSX_DM != null && SKSX_DM != "")
            {
                strwhere += " and  SKSX_DM = '" + SKSX_DM + "'";
            }
            if (ZSFS_DM != null && ZSFS_DM != "")
            {
                strwhere += " and  ZSFS_DM = '" + ZSFS_DM + "'";
            }
            if (FDataSource != null && FDataSource != "")
            {
                strwhere += " and  FDataSource = '" + FDataSource + "'";
            }
            if (NSRBM != null && NSRBM != "")
            {
                strwhere += " and  NSRBM like '%" + NSRBM + "%'";
            }
            //if (NSRSBH != null && NSRSBH != "")
            //{
            //    strwhere += " and  NSRSBH like '%" + NSRSBH + "%'";
            //}
            if (NSRMC != null && NSRMC != "")
            {
                strwhere += " and  NSRMC like '%" + NSRMC + "%'";
            }
            if (HY_DM != null && HY_DM != "")
            {
                strwhere += " and  HY_DM = '" + HY_DM + "'";
            }
            if (taxClassNo != null && taxClassNo != "")
            {
                strwhere += " and  ZSXM_DM = '" + taxClassNo + "'";
            }
            if (YSKM_DM != null && YSKM_DM != "")
            {
                strwhere += " and  YSKM_DM = '" + YSKM_DM + "'";
            }
            VSBZSXXList = GetVSBZSXXListList(strwhere, pageIndex);

            return PartialView("TaxManagerListPartial", VSBZSXXList);
        }

       

        public ActionResult GSImportDateEdit()
        {
            return View();
        }

        public ActionResult GSImportDate()
        {
            try
            {
                string backname = string.Empty;
                string svrFileName = string.Empty;
                string uploadoffiepath = string.Empty;


                UploadedFile[] uploadFiles = UploadControlExtension.GetUploadedFiles("upload");
                List<Model.GS_SB_ZSXX> SB_ZSXXList = new List<Model.GS_SB_ZSXX>();
                DataTable dt = new DataTable();
                if (uploadFiles.Length > 0 && null != uploadFiles)
                {
                    for (int i = 0; i < uploadFiles.Length; i++)
                    {
                        UploadedFile upload = uploadFiles[i];
                        if (upload != null)
                        {
                            backname = upload.FileName.Substring(upload.FileName.LastIndexOf('.') + 1);
                            string entityNo = GlobalConstant.ENTITYNO;
                         
                            string dest_dir = "/Publish/" + entityNo + "/GS_Date/" + DateTime.Now.Year + DateTime.Now.Month + "/";

                            svrFileName = "GS_DATA" + DateTime.Now.ToString("yyyyMMddHHmm");

                            uploadoffiepath = Server.MapPath(Url.Content("~") + dest_dir);//上传文件的路径

                            if (!Directory.Exists(uploadoffiepath))//如果不存在就创建
                            {
                                Directory.CreateDirectory(uploadoffiepath);
                            }

                            

                            upload.SaveAs(uploadoffiepath + svrFileName + "." + backname);

                            BLL.SB_ZSXX SB_ZSXXBLL = new BLL.SB_ZSXX();
                            DataSet ds = SB_ZSXXBLL.ReadExcelTitle(uploadoffiepath + svrFileName + "." + backname);

                            foreach (DataColumn dc in ds.Tables[0].Columns)
                            {
                                dt.Columns.Add(dc.ColumnName);
                            }

                            foreach (DataColumn dc in ds.Tables[0].Columns)
                            {
                                if (dc.ColumnName.Contains("_DM"))
                                {
                                    dc.ColumnName = dc.ColumnName.Replace("_DM", "_MC");
                                }
                            }

                            Model.GS_SB_ZSXX GS_SB_ZSXXModel = new Model.GS_SB_ZSXX();
                            PropertyInfo[] propertys = GS_SB_ZSXXModel.GetType().GetProperties();
                            foreach (PropertyInfo pi in propertys)
                            {
                                string tempName = pi.Name;
                                if (!dt.Columns.Contains(tempName))
                                {
                                    dt.Columns.Add(tempName);
                                    if (pi.PropertyType == typeof(DateTime?))
                                    {
                                        dt.Columns[tempName].DataType = typeof(DateTime);
                                    }
                                    else if (pi.PropertyType == typeof(decimal?))
                                    {
                                        dt.Columns[tempName].DataType = typeof(decimal);
                                    }
                                    else
                                    {
                                        dt.Columns[tempName].DataType = pi.PropertyType;
                                    }
                                }
                                else
                                {
                                    if (pi.PropertyType == typeof(DateTime?))
                                    {
                                        dt.Columns[tempName].DataType = typeof(DateTime);
                                    }
                                    else if (pi.PropertyType == typeof(decimal?))
                                    {
                                        dt.Columns[tempName].DataType = typeof(decimal);
                                    }
                                    else
                                    {
                                        dt.Columns[tempName].DataType = pi.PropertyType;
                                    }
                                }
                            }

                            int z = 0;
                            foreach (DataRow dr in ds.Tables[0].Rows)
                            {
                                DataRow dr2 = dt.NewRow();
                                foreach (DataColumn dc in dr.Table.Columns)
                                {
                                    if (dt.Columns.Contains(dc.ColumnName))
                                    {
                                        try
                                        {
                                            if (dr[dc.ColumnName].ToString() != "")
                                            {
                                                dr2[dc.ColumnName] = dr[dc.ColumnName];
                                            }
                                            else
                                            {
                                                dr2[dc.ColumnName] = DBNull.Value;
                                            }
                                        }
                                        catch
                                        {
                                            string aa = dc.ColumnName;
                                        }

                                    }
                                   
                                }
                                z++;
                                dr2["FExcelFileName"] = svrFileName;
                                dr2["FExcelRowNumber"] = z;
                                dr2["ZSXH"] = z;
                                
                                dt.Rows.Add(dr2);
                            }
                        }
                    }
                }
               

                BLL.GS_SB_ZSXX GS_SB_ZSXXBLL = new BLL.GS_SB_ZSXX();
                //ViewData["jsAlert"] = GS_SB_ZSXXBLL.ImportDate(dt);

                return View("GSImportDateEdit");
            }
            catch
            {
                ViewData["jsAlert"] = "导入异常！";
                return View("GSImportDateEdit");
            }
        }


        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.VSBZSXX> GetVSBZSXXListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.VSBZSXX VSBZSXXBLL = new BLL.VSBZSXX();
            string fiedls = "ZSXH";
            int total;
            DataSet ds = commonBll.GetListByPage2("fund.V_SB_ZSXX", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "", out total);
            List<Model.VSBZSXX> taskList = VSBZSXXBLL.DataTableToList(ds.Tables[0]);
            List<Model.VSBZSXX> taskAllList = new List<Model.VSBZSXX>();
            total = VSBZSXXBLL.GetRecordCount(strWhere);
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                taskAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (taskList.Count > i)
                {
                    taskAllList[((pageIndex - 1) * pageRowSize) + i] = taskList[i];
                }
            }

            return taskAllList;
        }

    }
}
